﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using ArtificalInteligence;
using SSS.AlphaNN.Components;
using AlphaNN.Forms;
using SerialozLibrary;

namespace SSS.AlphaNN.Forms
{
    public partial class MainFormClassic : Form
    {
        public MainFormClassic()
        {
            InitializeComponent();

            //Здесь мы добавляем наш компонент рабочая область на форму,
            //при этом мы сначало делаем цвет формы как у рибона
            //затем мы меняем цвет у tabControl
            //вкладываем на табконтрол нашу рабочую область и инициализируем список рабочих областей, 
            //поскольку их может быть много
            this.BackColor = SystemColors.GradientActiveCaption;
            tabControl1.TabPages[0].BackColor = SystemColors.GradientActiveCaption;
            tabControl1.SelectedTab.Controls.Add(wrkGround);
            wrkGround.Dock = DockStyle.Fill;

            liWorkGround = new List<WorkGround>();
            liWorkGround.Add(wrkGround);
            //количество нейронных сетей
            _countNeuronPage = 0;
        }

        /// <summary>
        /// Вывод окна информации о программе
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            ShowAboutForm();
        }

        /// <summary>
        /// Нажатие кнопки "Нейрон"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonNeuron_Click(object sender, EventArgs e)
        {
            tool = Tools.Neuron;
            ToolSwitch();
        }

        /// <summary>
        /// Нажатие кнопки "Указатель"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonPointer_Click(object sender, EventArgs e)
        {
            tool = Tools.Pointer;
            ToolSwitch();
        }

        /// <summary>
        /// Нажатие кнопки "Связь"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonContact_Click(object sender, EventArgs e)
        {
            tool = Tools.Contact;
            ToolSwitch();
        }

        /// <summary>
        /// Нажатие кнопки "Учитель"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonTeacher_Click(object sender, EventArgs e)
        {
            tool = Tools.Teacher;
            ToolSwitch();
        }

        /// <summary>
        /// Нажатие кнопки "DB"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonDB_Click(object sender, EventArgs e)
        {
            tool = Tools.DB;
            ToolSwitch();
        }

        /// <summary>
        /// Нажатие кнопки "FO"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonFO_Click(object sender, EventArgs e)
        {
            tool = Tools.FO;
            ToolSwitch();
        }

        /// <summary>
        /// Нажатие кнопки "Запустить"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonStart_Click(object sender, EventArgs e)
        {
            RunMethod(sender, e);
        }

        /// <summary>
        /// Нажатие кнопки "Пауза"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonPauseNetwork_Click(object sender, EventArgs e)
        {
            AbortAllThread();
        }

        /// <summary>
        /// Нажатие кнопки "Стоп"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonStopNetwork_Click(object sender, EventArgs e)
        {
            AbortAllThread();
        }

        /// <summary>
        /// Нажатие кнопки "Обучить"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonTeachNetwork_Click(object sender, EventArgs e)
        {
            LearningMethod(sender, e);
        }

        /// <summary>
        /// Убирает всё то, что натворил пользователь, понимаешь, в рабочей области
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonClearNetwork_Click(object sender, EventArgs e)
        {
            ClearWorkGroundFixed(sender, e);
        }

        /// <summary>
        /// Нажатие кнопки масштаба 50%
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonSсаle50_Click(object sender, EventArgs e)
        {
            ScaleWorkGround(-50);
        }

        /// <summary>
        /// Нажатие кнопки масштаба 100%
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonSсalе100_Click(object sender, EventArgs e)
        {
            ScaleWorkGround(1);
        }

        /// <summary>
        /// Нажатие кнопки "Создать"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemCreate_Click(object sender, EventArgs e)
        {
            CreateTabPage(sender, e);
        }

        /// <summary>
        /// Нажатие кнопки "Открыть"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemOpen_Click( object sender, EventArgs e )
        {
            //CreateTabPage( sender, e );
            //OpenNeuronNetwork(sender, e);
            int i = tabControl1.SelectedIndex;
            
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Бинарные мозги нейросети (*.bin)|*.bin|Все файлы (*.*)|*.*";
            if ( ofd.ShowDialog() == DialogResult.OK )
            {
                liWorkGround[i].LoadGhostDump(ofd.FileName);
                liWorkGround[i].Refresh();
                liWorkGround[i].FileName = ofd.FileName;
                tabControl1.SelectedTab.Text = ofd.FileName;
            }

           
        }

        /// <summary>
        /// Нажатие кнопки "Сохранить"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemSave_Click(object sender, EventArgs e)
        {
            SaveNeuronNetwork(sender, e);

            

        }

        /// <summary>
        /// Нажатие кнопки "Сохранить как"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemSaveAs_Click(object sender, EventArgs e)
        {
            SaveAsNeuronNetwork(sender, e);
        }

        /// <summary>
        /// Нажатие кнопки "Закрыть"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemCloseForm_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void закрытьНейроннуюСетьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClosePage(sender, e);
        }

        private void menuItemParameters_Click(object sender, EventArgs e)
        {
            (new WeightsGenerationForm(liWorkGround[tabControl1.SelectedIndex].GetListLink)).ShowDialog();
        }

        private void генерироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ArtificalInteligence.NeuronNetworkGenerator nnCreator = new NeuronNetworkGenerator(typeof(Neuron), new System.Drawing.Size(40, 40),
                new Point(30, 30));
            int[] cN = (new int[] {2, 4, 1});
            ser_tmp_ ss = nnCreator.generateStream(3, cN, 1, new object[] {0, 1, 0});
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;

            bf.Serialize(ms, ss);
            int i = tabControl1.SelectedIndex;
            ms.Position = 0;
            liWorkGround[i].LoadGhostDump(ms);
            liWorkGround[i].Refresh();

        }
    }
}
